﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
using NPOI.HSSF.UserModel;
using BusinessRule.ImportExcel;

public partial class ImportExcel_Import_Contact_Info :PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_UpLoad_Click(object sender, EventArgs e)
    {
        bool fileOK = false;
        string path = Server.MapPath("~/Temp/");
        if (fupExcel.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(fupExcel.FileName).ToLower();
            String[] allowedExtensions = { ".xlsx",".xls" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            try
            {
                fupExcel.SaveAs(path + fupExcel.FileName);
                Import_Contact_InfoManager icim = new Import_Contact_InfoManager();
                Stream stream = fupExcel.FileContent;
                HSSFWorkbook wb = new HSSFWorkbook(stream);
                HSSFSheet sheet = (HSSFSheet)wb.GetSheetAt(0);
                HSSFRow header = (HSSFRow)sheet.GetRow(0);
                DataTable dt = new DataTable();
                int cellCount = header.LastCellNum;
                for (int i = header.FirstCellNum; i < cellCount; i++)
                {
                    DataColumn column = new DataColumn(header.GetCell(i).StringCellValue);
                    dt.Columns.Add(column);
                }

                for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++)
                {
                    HSSFRow row = (HSSFRow)sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }
                    dt.Rows.Add(dataRow);
                }
                icim.ImportContact(dt,MyStateManager.DomainID,MyStateManager.Person_Name,MyStateManager.PersonID);
                //icim.ImportContact(path + fupExcel.FileName,
                //        "sheet1",
                //        "客户代码,客户名称,商圈分级,渠道,康师傅分管部门",
                //        "CUSTOMERS",
                //        "CODE,NAME,GEO_LEVEL_CODE,LOCAL_LEVEL1_CODE,ORG_ID", "");
                LabMessage1.Text = "文件上传成功.";
                LabMessage2.Text = "<b>原文件路径：</b>" + fupExcel.PostedFile.FileName + "<br />" +
                              "<b>文件大小：</b>" + Math.Round(fupExcel.PostedFile.ContentLength / 1024.0, 3) + "KB<br />" +
                              "<b>文件类型：</b>" + fupExcel.PostedFile.ContentType + "<br />";
                
            }
            catch (Exception ex)
            {
                LabMessage1.Text = "文件上传不成功.";
            }
        }
        else
        {
            LabMessage1.Text = "只能够上传Excel.";
        }
    }
}